<template>
    <div class="bottom-bar">
      <div class="button" @click="checkAll">
        <check-button class="check-button"
                      :is-checked="isSelectAll">
        </check-button>
        <div>全选</div>
      </div>
      <div class="total-price">
        合计：{{totalPrice}}
      </div>
      <div class="calculate" @click="calcClick">
        去计算({{counted}})
      </div>
    </div>
</template>

<script>
import CheckButton from 'components/content/checkButton/CheckButton'
import { mapGetters } from 'vuex'
export default {
  name: 'CartBottomBar',
  components: {
    CheckButton
  },
  computed: {
    ...mapGetters(['cartList']),
    totalPrice () {
      //   return '￥' + this.$store.state.cartList.filter(item => {
      return '￥' + this.cartList.filter(item => { // mapGetters结构之后这样写
        return item.isChecked
      }).reduce((preValue, item) => {
        return preValue + item.price * item.count
      }, 0).toFixed(2) // toFixed(2) 计算结果保留2位小数
    },
    counted () {
      return this.cartList.filter(item => item.isChecked).length
    },
    isSelectAll () {
      if (this.cartList.length === 0) return false
      // 1. filter会将数组全部遍历完
      // return !(this.cartList.filter(item => !item.isChecked).length)
      // 2. find只找到一个就不找了，性能会高一点
      return !(this.cartList.find(item => !item.isChecked))
      // 3.简单遍历，也会全部遍历完
      // for(let item of this.cartList){
      //     if(!item.checked){ // 没有选中的情况为真
      //             return false
      //         }
      //     }
      //         return true
    }
  },
  methods: {
    checkAll () {
      if (this.isSelectAll) { // 全部选中
        // eslint-disable-next-line no-return-assign
        this.cartList.forEach(item => item.isChecked = false)
      } else { // 部分或全部不选中
        // eslint-disable-next-line no-return-assign
        this.cartList.forEach(item => item.isChecked = true)
      }
    },
    calcClick () {
      if (!this.isSelectAll) {
        this.$toast.show('请添加商品!', 2000)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.bottom-bar{
  height: 40px;
  line-height: 40px;
  background-color: #eee;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  text-align: center;
  .button{
    width: 80px;
    display: flex;
    margin-left: 8px;
    align-items: center;
    .check-button{
      width: 20px;
      height: 20px;
      line-height: 20px;
      margin-right: 5px;
    }
  }
  .total-price {
    margin-left: 20px;
    flex: 1;
  }
  .calculate {
    width: 100px;
    background-color: var(--color-tint);
  }

}
</style>
